package com.schoolai.schoolset.rabbitmq;

import com.rabbitmq.client.Channel;
import com.schoolai.entity.SchoolSetSmsLog;
import com.schoolai.entity.rabbitmq.SmsInfo;
import com.schoolai.schoolset.service.ISchoolSetSmsLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;

/**
 * Copyright(C),2019-2025，XX公司
 * FileName:SmsQueueListenning
 * Author:bobby
 * 创建时间：2025/11/12 11:02
 * Description:短信队列监听
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 */
@Component
@Slf4j
@RabbitListener(queues = "SMSQueue",containerFactory = "rabbitListenerContainerFactory")
public class SmsQueueListener {
    @Autowired
    ISchoolSetSmsLogService ISchoolSetSmsLogService;
    @Transactional(rollbackFor = Exception.class)
    @RabbitHandler//监听队列中的短信息
    public void SMSQueueReceiver(SmsInfo smsInfo, Channel channel, Message message) throws IOException {
        SchoolSetSmsLog smsLog = new SchoolSetSmsLog();
        smsLog.setPhone(smsInfo.getPhone());
        smsLog.setContent(smsInfo.getContent());
        smsLog.setSendedTime(smsInfo.getSendTime());
        smsLog.setCreatedTime(new Date());
        ISchoolSetSmsLogService.saveOrUpdate(smsLog);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("提交网关：手机号码：{},信息内容：{},发送时间：{}", smsInfo.getPhone(), smsInfo.getContent(), smsInfo.getSendTime());
    }
}
